// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
/**
 * 
 * @param {*} event 
 * @param {*} context 
 */
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const c = cloud.database().collection('comments')
  console.log(event)
  let id = event.id //学习资料id
  let skipNum = event['skip'] || 0 //跳过的条数
  let limitNum = event["limitnum"] || 5 //第一次获取评论时的限制数，默认为5

  let resdata = await c.where({
    "resourceId": id,
    "parentId": ""
  }).skip(skipNum).limit(limitNum).get().then(res => {
    for(let i = 0, l = res.data.length; i < l; i++){
      let time = res.data[i]['date']
      if(time){
        time = "" + time.getFullYear().toString() + "-" + (time.getMonth() + 1).toString() + "-" + time.getDate().toString() + ' ' + time.getHours().toString() + ":" + time.getMinutes().toString()
      }else{
        time = ''
      }
      res.data[i]['date'] = time
    }
    return res.data
  })
  console.log('resdata = ', resdata)
  return resdata
}